package com.atguigu.gmall.realtime.common.util;

/*import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.common.constant.Constant;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(10);
        config.setMinIdle(10);
        config.setTestOnCreate(true);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(false);
        config.setMaxWaitMillis(10 * 1000);
        jedisPool = new JedisPool(config, Constant.REDIS_HOST, Constant.REDIS_POST);
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }

    public static void closeJedis(Jedis jedis){
        if (jedis != null){
            jedis.close();
        }
    }

    *//**
     * 写入维度数据
     *
     * 将一条维度数据 写成 String类型的 kv
     *//*
    public static void writeDim(Jedis jedis , String tableName , String rowKey , JSONObject dimJsonObj ){
        String key = getKey(tableName, rowKey);
        jedis.setex( key , Constant.ONE_DAY_SECOND , dimJsonObj.toJSONString()) ;
    }


    *//**
     * 读取维度数据
     *//*
    public static JSONObject readDim(Jedis jedis , String tableName , String rowKey ){
        String key = getKey(tableName, rowKey);
        String value = jedis.get(key);

        return value!= null ? JSONObject.parseObject(value ) : null ;
    }


    *//**
     * 删除维度数据
     *//*
    public static void delDim(Jedis jedis , String tableName , String rowKey){
        String key = getKey(tableName, rowKey);
        jedis.del( key ) ;
    }



    public static String getKey(String tableName, String rowKey){
        return tableName + ":" + rowKey ;
    }

}*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.common.constant.Constant;
import com.sun.xml.bind.v2.runtime.reflect.opt.Const;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.concurrent.ExecutionException;

/**
 * @author WEIYUNHUI
 * @date 2024/8/12 9:16
 */
public class RedisUtil {

    /**
     * 获取Redis 异步连接的方法
     */
    public static StatefulRedisConnection<String, String> getAsyncConnection(){
        RedisClient redisClient = RedisClient.create(Constant.REDIS_URL);
        StatefulRedisConnection<String, String> asyncConnect = redisClient.connect();
        return asyncConnect;
    }


    /**
     * 关闭Redis 异步连接的方法
     */
    public static void closeAsyncConnection(StatefulRedisConnection<String, String> asyncConnect){
        if (asyncConnect != null && asyncConnect.isOpen()) asyncConnect.close();
    }


    /**
     * 异步读取
     */
    public static JSONObject readDimAsync(StatefulRedisConnection<String, String> asyncConnect , String tableName , String rowKey ){
        String key = getKey(tableName, rowKey);
        String value = null;
        try {
            value = asyncConnect.async().get(key).get();
            return value!= null ? JSONObject.parseObject(value ) : null ;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 异步写入
     */
    public static void writeDimAsync(StatefulRedisConnection<String, String> asyncConnect , String tableName , String rowKey , JSONObject dimJsonObj ){
        String key = getKey(tableName, rowKey);
        asyncConnect.async().setex( key , Constant.ONE_DAY_SECOND , dimJsonObj.toJSONString()) ;
    }


    //连接池对象
    private static JedisPool jedisPool ;

    //初始化连接池对象
    static{
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(10);
        config.setMinIdle(10);
        config.setTestOnCreate(true);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(false);
        config.setMaxWaitMillis(10 * 1000);

        jedisPool = new JedisPool(config ,Constant.REDIS_HOST , Constant.REDIS_POST);
    }

    public static Jedis getJedis(){
        return jedisPool.getResource() ;
    }

    public static void closeJedis(Jedis jedis){
        if(jedis != null ) jedis.close();
    }

    /**
     * 写入维度数据
     *
     * 将一条维度数据 写成 String类型的 kv
     */
    public static void writeDim(Jedis jedis , String tableName , String rowKey , JSONObject dimJsonObj ){
        //String key  = tableName + ":" + rowKey ;
        //jedis.set( key , dimJsonObj.toJSONString() ) ;
        //jedis.expire( key ,  Constant.ONE_DAY_SECONDS ) ;
        String key = getKey(tableName, rowKey);
        jedis.setex( key , Constant.ONE_DAY_SECOND , dimJsonObj.toJSONString()) ;
    }

    /**
     * 读取维度数据
     */
    public static JSONObject readDim(Jedis jedis , String tableName , String rowKey ){
        String key = getKey(tableName, rowKey);
        String value = jedis.get(key);

        return value!= null ? JSONObject.parseObject(value ) : null ;
    }

    /**
     * 删除维度数据
     */

    public static void delDim(Jedis jedis , String tableName , String rowKey){
        String key = getKey(tableName, rowKey);
        jedis.del( key ) ;
    }


    public static String getKey(String tableName, String rowKey){
        return tableName + ":" + rowKey ;
    }
}
